package com.jhsw.roadsun.module.business.dal.mysql.expressage;

import com.jhsw.roadsun.framework.common.pojo.PageResult;
import com.jhsw.roadsun.framework.mybatis.core.mapper.BaseMapperX;
import com.jhsw.roadsun.framework.mybatis.core.query.MPJLambdaWrapperX;
import com.jhsw.roadsun.module.business.controller.admin.expressage.vo.ExpressagePageReqVO;
import com.jhsw.roadsun.module.business.controller.admin.expressage.vo.ExpressageRespVO;
import com.jhsw.roadsun.module.business.controller.app.expressage.vo.AppExpressagePageReqVO;
import com.jhsw.roadsun.module.business.controller.app.expressage.vo.AppExpressageSimpleRespVO;
import com.jhsw.roadsun.module.business.dal.dataobject.expressage.ExpressageDO;
import com.jhsw.roadsun.module.business.dal.dataobject.user.BizUserDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

/**
 * 寄件信息 Mapper
 *
 * @author Ysugar
 */
@Mapper
public interface ExpressageMapper extends BaseMapperX<ExpressageDO> {

    default PageResult<ExpressageRespVO> selectPage(ExpressagePageReqVO reqVO) {
        return selectJoinPage(reqVO, ExpressageRespVO.class, new MPJLambdaWrapperX<ExpressageDO>()
                .selectAll(ExpressageDO.class)
                .selectAs(BizUserDO::getNickname, ExpressageRespVO::getNickname)
                .selectAs(BizUserDO::getMobile, ExpressageRespVO::getMobile)
                .leftJoin(BizUserDO.class, BizUserDO::getId, ExpressageDO::getUserId)
                .likeIfPresent(ExpressageDO::getOrderNo, reqVO.getOrderNo())
                .betweenIfPresent(ExpressageDO::getCreateTime, reqVO.getCreateTime())
                .eqIfPresent(ExpressageDO::getAuditStatus, reqVO.getAuditStatus())
                .betweenIfPresent(ExpressageDO::getAuditDatetime, reqVO.getAuditDatetime())
                .likeIfPresent(BizUserDO::getNickname, reqVO.getNickname())
                .likeIfPresent(BizUserDO::getMobile, reqVO.getMobile())
                .eqIfPresent(ExpressageDO::getCountry, reqVO.getCountry())
                .eqIfPresent(ExpressageDO::getShippingType, reqVO.getShippingType())
                .eqIfPresent(ExpressageDO::getCustomsFiling, reqVO.getCustomsFiling())
                .eqIfPresent(ExpressageDO::getSendCompany, reqVO.getSendCompany())
                .eqIfPresent(ExpressageDO::getRecipientsCompany, reqVO.getRecipientsCompany())
                .eqIfPresent(ExpressageDO::getPayMethod, reqVO.getPayMethod())
                .eqIfPresent(ExpressageDO::getCustomerAccount, reqVO.getCustomerAccount())
                .eqIfPresent(ExpressageDO::getExpressageOrder, reqVO.getExpressageOrder())
                .orderByDesc(ExpressageDO::getId));
    }

    default PageResult<AppExpressageSimpleRespVO> appGetExpressagePage(AppExpressagePageReqVO reqVO, Long userId){
        return selectJoinPage(reqVO, AppExpressageSimpleRespVO.class, new MPJLambdaWrapperX<ExpressageDO>()
                .selectAs(ExpressageDO::getId, AppExpressageSimpleRespVO::getId)
                .selectAs(ExpressageDO::getOrderNo, AppExpressageSimpleRespVO::getOrderNo)
                .selectAs(ExpressageDO::getCountry, AppExpressageSimpleRespVO::getCountry)
                .selectAs(ExpressageDO::getShippingType, AppExpressageSimpleRespVO::getShippingType)
                .selectAs(ExpressageDO::getCustomsFiling, AppExpressageSimpleRespVO::getCustomsFiling)
                .selectAs(ExpressageDO::getPayMethod, AppExpressageSimpleRespVO::getPayMethod)
                .selectAs(ExpressageDO::getAuditStatus, AppExpressageSimpleRespVO::getAuditStatus)
                .selectAs(ExpressageDO::getCreateTime, AppExpressageSimpleRespVO::getCreateTime)
                .eqIfPresent(ExpressageDO::getUserId, userId)
                .likeIfPresent(ExpressageDO::getOrderNo, reqVO.getOrderNo())
                .eqIfPresent(ExpressageDO::getCountry, reqVO.getCountry())
                .eqIfPresent(ExpressageDO::getPayMethod, reqVO.getPayMethod())
                .eqIfPresent(ExpressageDO::getAuditStatus, reqVO.getAuditStatus())
                .betweenIfPresent(ExpressageDO::getCreateTime, reqVO.getCreateTime()));
    }

    @Select("select count(1) from business_expressage be where be.deleted = 0 and be.user_id = #{param1}")
    Long getExpressageCountByUserId(Long userId);
}